package chess;

import java.awt.Color;
import java.io.FileWriter;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
/**
 *
 * @author Alejandro
 */


public class Board extends javax.swing.JFrame {

    int n,size;
    JButton [][] board;
    Token [][] tokens;
    int [] parameters;
    boolean turn, _GoRdy;
    int color = 0;
    int _xInicial, _xFinal, _yInicial, _yFinal;
    
    public Board() {
        this.n=4;
        size = 400/n;
        board= new JButton[n][n];
        tokens = new Token [n][n];
        _GoRdy = false;
        parameters = new int [4];
        parameters[0] = 0;
        parameters[1] = -1;// if a token is clicked
        parameters[3] = 1; // white/#black player's turn
        initComponents();
        
    }

    // <editor-fold defaultstate="collapsed" desc="Codigo">
    private void initComponents() {
        try{
            FileWriter fwriter = new FileWriter("logActual.txt");
            fwriter.write("");
            fwriter.close();
        }catch (Exception e){
            System.err.println("Error: " + e.getMessage());
        }
        Pane = new javax.swing.JLayeredPane();
        
        ImageIcon icon = new ImageIcon("src/imagenes/Warcraft3034wallpapers.jpg");
        wallpaper = new javax.swing.JLabel();
        wallpaper.setBounds(0, 0, 733, 550);
        wallpaper.setIcon(icon);
        Pane.add(wallpaper, javax.swing.JLayeredPane.DEFAULT_LAYER);
        
        
        close = new javax.swing.JButton();
        close.setText("Cerrar");
        close.setForeground(Color.white);
        close.setBackground(Color.black);
        close.setBounds(600, 500, 100, 25);
        Pane.add(close, new Integer(1));

        
        close.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dispose();
            }
            });

        
        // Funcion para colocar las fichas
            /*
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                button = new javax.swing.JButton();
                button.setBounds(j*size+size, i*size+size, size, size);  
                Pane.add(button, new Integer(1));
                board[j][i]=button;
                if ((i%2==0&&j%2==0)||(i%2!=0&&j%2!=0)){ 
                    button.setBackground(Color.red.darker());
                    if (i > 3){
                        button.setBackground(Color.GREEN.darker());
                    }
                }
                else{
                    button.setBackground(Color.black);
                }
                if (i > 5){
                    color = 1;
                }
                if (i == 0 || i == 7){
                    if (j == 0 || j == 7){
                        tokens[j][i] = new Rook (j,i,color,board,tokens);
                    }
                    if (j == 1 || j == 6){
                        tokens[j][i] = new Knight (j,i,color,board,tokens);
                    }
                    if (j == 2 || j == 5){
                        tokens[j][i] = new Bishop (j,i,color,board,tokens);
                    }
                    if (j == 3){
                        tokens[j][i] = new Queen (j,i,color,board,tokens);
                    }
                    if (j == 4){
                        tokens[j][i] = new King (j,i,color,board,tokens);
                    }
                }
                if (i == 1 || i == 6){
                    tokens[j][i] = new Pawn (j,i,color,board,tokens);
                }
                button.addActionListener(
                        new TokenController(j,i,board,tokens,parameters));
            }
        }*/

        // tablero 4x4
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                button = new javax.swing.JButton();
                button.setBounds(j*size+size, i*size+size, size, size);
                Pane.add(button, new Integer(1));
                board[j][i]=button;
                if ((i%2==0&&j%2==0)||(i%2!=0&&j%2!=0)){
                    button.setBackground(Color.GREEN.darker());
                    if (i > 3){
                        button.setBackground(Color.red.darker());
                    }
                }
                else{
                    button.setBackground(Color.black);
                }
                if (i > 1){
                    color = 1;
                }
                if (i == 0 && j == 0){
                    tokens[j][i] = new Pawn (j,i,color,board,tokens);
                }
                if (i == 0 && j == 1){
                    tokens[j][i] = new King (j,i,color,board,tokens);
                }
                if (i == 0 && j == 2){
                    tokens[j][i] = new Queen (j,i,color,board,tokens);
                }
                if (i == 0 && j == 3){
                    tokens[j][i] = new Knight (j,i,color,board,tokens);
                }
                //--------------------otro color----------------------
                if (i == 3 && j == 0){
                    tokens[j][i] = new Pawn (j,i,color,board,tokens);
                }
                if (i == 3 && j == 1){
                    tokens[j][i] = new King (j,i,color,board,tokens);
                }
                if (i == 3 && j == 2){
                    tokens[j][i] = new Queen (j,i,color,board,tokens);
                }
                if (i == 3 && j == 3){
                    tokens[j][i] = new Knight (j,i,color,board,tokens);
                }
                button.addActionListener(
                        new TokenController(j,i,board,tokens,parameters));
            }
        }

        javax.swing.GroupLayout layout = 
                new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Pane, javax.swing.GroupLayout.PREFERRED_SIZE, 733, 
            javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Pane, javax.swing.GroupLayout.PREFERRED_SIZE, 550,
            javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>

    private javax.swing.JLayeredPane Pane;
    private javax.swing.JButton button;
    private javax.swing.JButton close;
    private javax.swing.JLabel wallpaper;

}



